/*************************************************************************
 * File Name:    Generate_Parentheses.cc
 * Author:       zero91
 * Mail:         jianzhang9102@gmail.com
 * Created Time: 2013/11/13 22:32:43
 * 
 * Description:  
 |------------------------------------------------------------------------
 | Problem: Generate Parentheses
 |
 | Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
 |
 | For example, given n = 3, a solution set is:
 |
 | "((()))", "(()())", "(())()", "()(())", "()()()"
 |------------------------------------------------------------------------
 ************************************************************************/

#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>

using namespace std;

class Solution {
public:
	vector<string> generateParenthesis(int n)
	{
		vector<string> ans;
		dfs(ans, "", n, n);
		return ans;
	}

private:
	void dfs(vector<string> &ans, string str, int left, int right)
	{
		if (left == 0 && right == 0) {
			ans.push_back(str);
			return;
        }
        
        if (left > 0) dfs(ans, str + "(", left - 1, right);
        if (right > left) dfs(ans, str + ")", left, right - 1); 
    }
};

